﻿using Cl.Service.ElsaWorkflow.Activity.ClAudit;
using Cl.Service.ElsaWorkflow.Activity.ClAudit.BookMarks;
using Cl.Service.ElsaWorkflow.Activity.ClNotification;
using Elsa.Persistence.EntityFramework.Core.Extensions;
using Elsa.Persistence.EntityFramework.MySql;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cl.Service.ElsaWorkflow
{
    public static class InitElsaWorkflow
    {
        public static IServiceCollection InitElsa(this IServiceCollection services, ConfigurationManager configurationManager)
        {
            var strConnection = configurationManager.GetConnectionString("db_elsa");
            var strMappingElsa = configurationManager.GetConnectionString("MappingElsa");

            if (string.IsNullOrEmpty(strConnection) || string.IsNullOrEmpty(strMappingElsa))
                return services;

            #region Elsa

            bool.TryParse(strMappingElsa, out bool mappingElsa);

            //如果使用elsa的时候打开注释
            services.AddElsa(
                options => options
                //.UseEntityFrameworkPersistence(ef => ef.UseMySql(strConnection), mappingElsa)
                .AddConsoleActivities()
                .AddHttpActivities()
                .AddActivity<NotificationActivity>()
                .AddActivity<AuditActivity>()//新创建的活动节点需要在这里设置；
            );

            services.AddBookmarkProvider<AuditActivityBookMarkProvider>();//审批节点等，需要配置；

            // Elsa API endpoints.
            services.AddElsaApiEndpoints();

            // For Dashboard.
            services.AddRazorPages();

            #endregion

            return services;
        }

    }
}
